package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.shopmall.Event.EventTest;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.LoginResponse;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.MD5;
import com.zadtek.shopmall.utils.SPUtil;
import com.zadtek.shopmall.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AbsActivity implements View.OnClickListener {

    private ImageView ivAvatar;
    private EditText etUserName,etPassword;
    private TextView tvLogin,tvForgetPassword,tvRegsister;
    private ImageView wx_login;
    @Override
    public int setView() {
        return R.layout.activity_login;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        initView();
        initLogic();
    }

    private void initLogic() {
        tvRegsister.setOnClickListener(this);
        tvForgetPassword.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        wx_login.setOnClickListener(this);
    }

    private void initView() {
        ivAvatar = (ImageView) findViewById(R.id.iv_avatar);
        etUserName = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        tvLogin =  (TextView) findViewById(R.id.tv_login);
        tvForgetPassword =  (TextView) findViewById(R.id.tv_forget_password);
        tvRegsister =  (TextView) findViewById(R.id.tv_regsister);
        wx_login = findViewById(R.id.wx_login);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_regsister:
                startActivity(new Intent(this,RegsisterActivity.class));
                break;
            case R.id.tv_forget_password:
                startActivity(new Intent(this,ForgetPasswordActivity.class));
                break;
            case R.id.tv_login:
                String userName = etUserName.getText().toString();
                String password = etPassword.getText().toString();
                if(TextUtils.isEmpty(userName)){
                    ToastUtil.showToast("请输入用户名！");
                    break;
                }
                if(TextUtils.isEmpty(password)){
                    ToastUtil.showToast("请输入密码！");
                    break;
                }
                login(userName,password);
                break;
            case R.id.wx_login:

                break;
        }
    }


    private void login(final String username, final String password) {
        Map<String, Object> params = NetTool.newParams();
        params.put("username",username);
        params.put("password", MD5.getStringMD5(password));
        NetTool.getApi().login(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResponse<LoginResponse>>() {
                    @Override
                    public void accept(BaseResponse<LoginResponse> baseResponse) throws Exception {
                        if (baseResponse.getCode() == 0 && baseResponse.getData().isSuccess()) {
                            ToastUtil.showToast("登录成功！");
                            LoginResponse response = baseResponse.getData();
                            SPUtil.saveDate(LoginActivity.this, Const.IsLogin, true);
                            SPUtil.saveDate(LoginActivity.this, Const.UserId, response.getUid());
                            SPUtil.saveDate(LoginActivity.this, Const.UserToken, response.getUsertoken());
                            Const.userid = response.getUid();
                            Const.token = response.getUsertoken();
                            Const.isLogin = true;
                            Const.loginYM();
                            finish();
                            EventBus.getDefault().post(new EventTest(EventTest.LOGIN));
                            //startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        } else {
                            ToastUtil.showToast(baseResponse.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtil.showToast(throwable.getLocalizedMessage());
                    }
                });
    }

    @Subscribe
    public void OnEvent(EventTest event) {
        if(event.getCode() == EventTest.LOGIN) finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
